// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

const {ccclass, property} = cc._decorator;

@ccclass
export default class SkinCreate extends cc.Component {

    @property(cc.SpriteAtlas)
    animPlist: cc.SpriteAtlas = null;

    currAction: string = "wait";

    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        this.setAnimClip()
        this.playAnim("wait","down");
    }

    setAnimClip() {
        if (!this.animPlist) return;
        const animKey = this.animPlist.name.replace(".plist", "");
        const t_frames = this.animPlist.getSpriteFrames();
        const actionkey = {
            "attack":{dir:5,frame:6, sample: 12, wrapMode: cc.WrapMode.Loop},
            "move":{dir:5,frame:8, sample: 12, wrapMode: cc.WrapMode.Loop},
            "wait":{dir:5,frame:3, sample: 6, wrapMode: cc.WrapMode.Loop},
            "wound":{dir:5,frame:2, sample: 4, wrapMode: cc.WrapMode.Loop},
        }
        for (const key in actionkey) {
            const value = actionkey[key];
            let anim_name = animKey+"_" + key
            const outframes = t_frames.filter(item => !item.name.indexOf(anim_name));

            //outframes.sort((a, b) => parseInt(a.name.substring(13, 15)) - parseInt(b.name.substring(13, 15)));
            //console.log(anim_name,outframes);
            let dirTags = ["up", "down", "left", "leftdown","leftup", "right", "rightdown","rightup"];
            for (let i = 0; i < value.dir; i++) {
                const t_dir = i;
                let t_anim_name = anim_name+"_"+dirTags[t_dir];
                
                let tt_frames = outframes.filter(item => !item.name.indexOf(t_anim_name+"_"));
                if (tt_frames.length > 0) {
                    //console.log(t_anim_name,tt_frames)
                    let tt_name = key + "_" + dirTags[t_dir]
                    let clip = cc.AnimationClip.createWithSpriteFrames(tt_frames, value.sample);
                    clip.wrapMode = value.wrapMode;
                    this.node.getComponent(cc.Animation).addClip(clip, tt_name);
                }
                if (t_dir >= 2) {
                    t_anim_name = anim_name+"_"+dirTags[t_dir];
                    tt_frames = outframes.filter(item => !item.name.indexOf(t_anim_name+"_"));
                    if (tt_frames.length > 0) {
                        //console.log(t_anim_name,tt_frames)
                        let tt_name = key + "_" + dirTags[t_dir+3]
                        let clip = cc.AnimationClip.createWithSpriteFrames(tt_frames, value.sample);
                        clip.wrapMode = value.wrapMode;
                        //console.log(tt_name)
                        this.node.getComponent(cc.Animation).addClip(clip, tt_name);
                    }
                }
            }
            // if (outframes.length > 0) {
            //     const clip = cc.AnimationClip.createWithSpriteFrames(outframes, value.sample);
            //     clip.wrapMode = value.wrapMode;
            //     this.node.getComponent(cc.Animation).addClip(clip, value.name);
            // }
        }
    }
    playAnim(key: string,dir:string) {
        if (!this.animPlist) return;
        let needTurn = ["right", "rightdown","rightup"]
        this.node.scaleX = 1
        if (needTurn.includes(dir)) {
            this.node.scaleX = -1
        }
        let anim_name = key + "_" + dir;

        if (this.node.getComponent(cc.Animation).hasAnimationState(anim_name)) {
            const clipState = this.node.getComponent(cc.Animation).play(anim_name);
            const t_spr = clipState.clip.curveData.comps["cc.Sprite"].spriteFrame[0].value;
            this.node.getComponent(cc.Sprite).spriteFrame = t_spr;
            this.currAction = key;
        }
        
    }
    resetAnim() {
        this.node.getComponent(cc.Animation).play(this.currAction);
    }

    pause() {
        this.unscheduleAllCallbacks();
        this.node.getComponent(cc.Animation).pause();
    }

    resume() {
        this.node.getComponent(cc.Animation).resume();
    }

    //完成动作后播放
    animOnce(callback: Function) {
        const anim = this.node.getComponent(cc.Animation);
        anim.once(cc.Animation.EventType.FINISHED, () => {
            callback();
        });
    }

    // update (dt) {}
}
